package cn.fanyamin.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.ToolService;
import cn.fanyamin.domain.Account;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.User;

public class AccountAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(AddressAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		   
		
		User user = getUserFromSession(request);
        
		setCategories(request,UserCategory.CATEGORY_TYPE_BOOK);

	    List<Account> accountList = toolService.listAccount(user.getUserID());
	    request.setAttribute("accountList", accountList);
		logger.info("accountList: " + accountList);
		return new ActionForward("/WEB-INF/jsp/struts/tool/accounts.jsp");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		Account account = getAccount4Form(form);
		if(account == null) {
		    return mapping.findForward("failure");
		}
		User user = getUserFromSession(request);
		account.setUserID(user.getUserID());		
		int accountID = toolService.createAccount(account);
		logger.info("create account: " + account);
		account.setAccountID(accountID);
		return mapping.findForward("success");
	}
	
    private Account getAccount4Form(ActionForm form) {
		Account account = new Account();
		LazyValidatorForm accountForm = (LazyValidatorForm) form;

		account.setAccountID(NumberUtils.toInt((String) accountForm.get("accountID")));
		account.setUserName((String) accountForm.get("userName"));	
		account.setEmail((String) accountForm.get("email"));	
		account.setPassword((String) accountForm.get("password"));
		account.setSiteName((String)accountForm.get("siteName"));
		account.setSiteUrl((String)accountForm.get("siteUrl"));
		return account;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm accountFrom = (LazyValidatorForm)form;
        int accountID = NumberUtils.toInt((String)accountFrom.get("accountID"));
        Account account = toolService.retrieveAccount(accountID);
        request.setAttribute("account", account);
        return new ActionForward("/WEB-INF/jsp/struts/tool/accounts.jsp");
    }
    
        
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        ToolService toolService = getWebApp().getToolService();
        Account account = getAccount4Form(form);
        toolService.updateAccount(account);
        //request.setAttribute("accountType", account.getAccountType());
        logger.info("update account, account=" + account);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm accountFrom = (LazyValidatorForm)form;
        int accountID = NumberUtils.toInt((String)accountFrom.get("accountID"));
        toolService.deleteAccount(accountID);
        int accountType = NumberUtils.toInt(request.getParameter("accountType"));
        //request.setAttribute("accountType", accountType);
        logger.info("delete account, accountID=" + accountID);
        //return mapping.findForward("success");
        return mapping.findForward("success");
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add account");
    	
    	setCategories(request,UserCategory.CATEGORY_TYPE_BOOK);
    	
    	return new ActionForward("/WEB-INF/jsp/struts/tool/account.jsp");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm accountFrom = (LazyValidatorForm)form;
        int accountID = NumberUtils.toInt((String)accountFrom.get("accountID"));
        Account account = toolService.retrieveAccount(accountID);
        request.setAttribute("account", account);
        
        setCategories(request,UserCategory.CATEGORY_TYPE_BOOK);
        
        return new ActionForward("/WEB-INF/jsp/struts/tool/account.jsp");
    }
}
